
package org.itboys.mobile.service.mongo.web;

import com.google.common.collect.Maps;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.io.FileUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.entity.mongo.SundayApkInfo;
import org.itboys.mobile.util.apk.ApkUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SundayApkInfoService extends BaseMongoService<SundayApkInfo> {


	@Autowired
	private ResourceHolder resourceHolder;
	
	private static final long serialVersionUID = -5806434055381127192L;
	@Resource(name="mobileDs")
	private MongoDataSource sundayDataSource;
	@Override
	protected MongoDataSource getMongoDataSource() {
		return sundayDataSource;
	}
	@Override
	protected Class<SundayApkInfo> getEntityClass() {
		return SundayApkInfo.class;
	}
	
	/*
	 * 
	 * @param logs。更新日志
	 * @param type。类型：1，安卓用户版本，3，安卓管理版，3，苹果用户版，4，苹果管理版
	 * @param file。安卓APK文件
	 * @param versionCode。苹果版本号
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */

	public void uploadApk(String logs,Integer type,MultipartFile file,Integer versionCode,String downloadUrl) throws Exception{
		//上传苹果
		if(type==3||type==4){
			SundayApkInfo apkInfo = new SundayApkInfo();
			apkInfo.setType(type);
			apkInfo.setVersionCode(versionCode);
			apkInfo.setFileLogs(logs);
			apkInfo.setDownLoadUrl(downloadUrl);
			super.save(apkInfo);
		}
		//上传安卓
		if(type==1||type==2){
			String fileUploadPath = resourceHolder.getStringValue("fileUploadPath") + "/apk/android";
			String imgRoot = resourceHolder.getStringValue("imgRoot");
			if (file != null && !file.isEmpty()) {
				String filePath = FileUtils.FILE_SEPARATOR + file.getOriginalFilename();
				IOUtils.copyLarge(file.getInputStream(), new FileOutputStream(fileUploadPath + FileUtils.FILE_SEPARATOR + file.getOriginalFilename()));
				SundayApkInfo apkInfo = ApkUtil.getApkInfo(fileUploadPath + filePath);
				apkInfo.setFileMd5(DigestUtils.md5Hex(IOUtils.toByteArray(file.getInputStream())));
				apkInfo.setFileLogs(logs);
				apkInfo.setFizeSize(String.valueOf(file.getSize()));
				apkInfo.setDownLoadUrl(imgRoot + "/apk/android" + filePath);
				apkInfo.setType(type);
				super.save(apkInfo);
				File file1 = new File(fileUploadPath + filePath);
				if(file1.exists()){
					file1.renameTo(new File(fileUploadPath + FileUtils.FILE_SEPARATOR + "app_" + apkInfo.getVersionCode() + "_" + apkInfo.getVersionName() + ".apk"));
					apkInfo.setDownLoadUrl(imgRoot + "/apk/android" + FileUtils.FILE_SEPARATOR + "app_" + apkInfo.getVersionCode() + "_" + apkInfo.getVersionName() + ".apk");
					super.updateExceptEmpty(apkInfo.getId(),apkInfo);
					}
			}else{
				throw new ErrorException("请上传包");
			}
		}
	}

	/**
	 * 分页查询Apk信息
	 * @param request
	 * @return
	 */
	public PageResult<SundayApkInfo> select(HttpServletRequest request){
		Map<String,Object> param = ParamUtil.packageMongoExactParam(request,"type","versionCode");
		Map<String,String> containsparam = new HashMap<String,String>();
		return super.containpageQuery(request,containsparam,param);
	}
	//获取新版本APP
	public SundayApkInfo getApkInfo(Integer versionCode,Integer type) throws Exception{
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("versionCode >", versionCode);
		param.put("type", type);
		param.put("sort", "-versionCode");
		param.put("isDeleted", 0);
		List<SundayApkInfo> infoes = super.list(param);
		if(ListUtil.isNotNull(infoes)){
			return infoes.get(0);
		}
		return null;
	}
	
}

